//
//  SEGSettingPasswordViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/6/15.
//

#import "SEGSettingPasswordViewModel.h"
#import "SEGRequestManager.h"

@interface SEGSettingPasswordViewModel ()

@property (nonatomic, strong) SEGRequestManager *manager;

@end

@implementation SEGSettingPasswordViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        [self seg_createRequestConfigInfoSignal];
        [self seg_createValidPwdCommand];
    }
    return self;
}
- (void)seg_createRequestConfigInfoSignal {
    @weakify(self);
    self.reqConfigSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        [self.manager requestPasswordConfigInfo:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            [subscriber sendNext:result];
            [subscriber sendCompleted];
        }];
        
        return nil;
    }];
}
- (void)seg_createValidPwdCommand {
    @weakify(self);
    self.vaildCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSString *pwdStr) {
        @strongify(self);
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [self.manager requestPasswordIsVaild:pwdStr Finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    [subscriber sendNext:@""];
                    [subscriber sendCompleted];
                } else {
                    NSError *error = [NSError errorWithDomain:@"" code:codeType.integerValue userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                    [subscriber sendError:error];
                }
            }];
            
            return nil;
        }];
    }];
}

#pragma mark - Getter
- (SEGRequestManager *)manager {
    if (!_manager) _manager = [[SEGRequestManager alloc] init];
    return _manager;
}

@end
